﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Drawing;
using MVCClubSite.IDAL;
using MVCClubSite.Model;
using MVCClubSite.BLL;
using MVCClubSite.Web.ViewData;
using System.IO;
using MVCClubSite.Helpers;
using MVCClubSite.ActionResults;

namespace MVCClubSite.Web.Controllers
{
    public class ImagesController : BaseController
    {
        ImagesService _imagesService;
        LinksService _linksService;
        UserProfilesService _userProfilesService;

        public ImagesController()
            : this (null, null, null)
        {
        }

        public ImagesController(ImagesService imagesService, LinksService linksService, UserProfilesService userProfilesService)
        {
            _imagesService = imagesService ?? new ImagesService();
            _linksService = linksService ?? new LinksService();
            _userProfilesService = userProfilesService ?? new UserProfilesService();
        }

        public ActionResult OriginalImage(int id)
        {
            MVCClubSite.Model.Image file = _imagesService.GetImageByID(id);
            BinaryResult result = new BinaryResult();

            if (file == null)
            {
                file = new MVCClubSite.Model.Image();
                file.LargeImage = MVCClubSite.BLL.ImageHelper.ConvertImageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/imagenotfound-large.gif")));
            }

            result.ContentType = "image/jpeg";
            result.Data = file.OriginalImage;
            result.IsAttachment = false;

            return result;
        }

        public ActionResult ThumbnailImage(int id)
        {
            MVCClubSite.Model.Image file = _imagesService.GetImageByID(id);
            BinaryResult result = new BinaryResult();

            if (file == null)
            {
                file = new MVCClubSite.Model.Image();
                file.ThumbnailImage = MVCClubSite.BLL.ImageHelper.ConvertImageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/imagenotfound-thumbnail.gif")));
            }

            result.ContentType = "image/jpeg";
            result.Data = file.ThumbnailImage;
            result.IsAttachment = false;

            return result;
        }

        public ActionResult LargeImage(int id)
        {
            MVCClubSite.Model.Image file = _imagesService.GetImageByID(id);
            BinaryResult result = new BinaryResult();

            if (file == null)
            {
                file = new MVCClubSite.Model.Image();
                file.LargeImage = MVCClubSite.BLL.ImageHelper.ConvertImageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/imagenotfound-large.gif")));
            }

            result.ContentType = "image/jpeg";
            result.Data = file.LargeImage;
            result.IsAttachment = false;

            return result;
        }

        public ActionResult LinkImage(int id)
        {
            Link link = _linksService.GetLinkByID(id);

            BinaryResult result = new BinaryResult();

            if (link == null || link.Image == null)
            {
                link = new MVCClubSite.Model.Link();
                link.Image = MVCClubSite.BLL.ImageHelper.ConvertImageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/imagenotfound-thumbnail.gif")));
            }

            result.ContentType = "image/jpeg";
            result.Data = link.Image;
            result.IsAttachment = false;

            return result;
        }

        public ActionResult Avatar(string id)
        {
            UserProfile profile = _userProfilesService.GetUserProfile(new Guid(id));
            BinaryResult result = new BinaryResult();
            if (profile == null || profile.Avatar == null)
            {
                profile = new MVCClubSite.Model.UserProfile();
                profile.Avatar = MVCClubSite.BLL.ImageHelper.ConvertImageToByteArray(System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/imagenotfound-thumbnail.gif")));
            }
            result.ContentType = "image/jpeg";
            result.Data = profile.Avatar;
            result.IsAttachment = false;

            return result;
        }
    }
}
